package pqt;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;

public class Main {

	private JFrame frame;
	private JTextField textField;
	private Connection conexion;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws SQLException 
	 */
	public Main() throws SQLException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws SQLException 
	 */
	private void initialize() throws SQLException {
		
		conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/systel","root","4881315");	
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(126, 63, 204, 31);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JButton btnInsertar = new JButton("INSERTAR");
		btnInsertar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			/*	try {
					insertarBloqueAtributo();
				} catch (NumberFormatException | IOException | SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			*/
				try {
					insertarFallasAtributo();
				} catch (NumberFormatException | IOException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnInsertar.setBounds(180, 192, 89, 23);
		frame.getContentPane().add(btnInsertar);
		
		textField_1 = new JTextField();
		textField_1.setBounds(127, 112, 204, 31);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblIdBloque = new JLabel("ID BLOQUE");
		lblIdBloque.setBounds(26, 71, 64, 14);
		frame.getContentPane().add(lblIdBloque);
		
		JLabel lblIdAtributo = new JLabel("ID ATRIBUTO");
		lblIdAtributo.setBounds(26, 120, 78, 14);
		frame.getContentPane().add(lblIdAtributo);
	}

	private void insertarBloqueAtributo() throws NumberFormatException, IOException, SQLException {
		BufferedReader reader = null;
		reader = new BufferedReader(new FileReader("C:\\Users\\Mariano\\Desktop\\bloqueatributo.txt"));
		String line = null;
		String[] linea = null;
		int idAtributo = 0;
		String Atributo = null;
		int idBloque = 0;
		String Bloque = null;
			while ((line = reader.readLine()) != null) {
				linea = line.split(",");
				idAtributo = Integer.parseInt(linea[0]);
				Atributo = linea[1];
				idBloque = Integer.parseInt(linea[2]);
				Bloque = linea[3];
			//	System.out.println(+idAtributo+","+Atributo+","+idBloque+","+Bloque);
				
				String query = "insert into bloqueatributo values("+idBloque+", "+idAtributo+")";
					PreparedStatement modificar = conexion.prepareStatement(query);
					modificar.executeUpdate();
					System.out.println(query);
		}			
	}	

	private void insertarFallasAtributo() throws NumberFormatException, IOException, SQLException {
		BufferedReader reader = null;
		reader = new BufferedReader(new FileReader("C:\\Users\\Mariano\\Desktop\\falla.txt"));
		String line = null;
		String[] linea = null;
		int idBloque = 0;
		String descripcion = null;
			while ((line = reader.readLine()) != null) {
				linea = line.split(",");
				idBloque = Integer.parseInt(linea[0]);
				descripcion = linea[1];
//				System.out.println(+idBloque+","+descripcion);
				
				String query = "insert into falla values(null, '"+descripcion+"', "+idBloque+")";
					PreparedStatement modificar = conexion.prepareStatement(query);
					modificar.executeUpdate();
					System.out.println(query);
			
			}	
	}
}
